package top.xiaoazi.futureletterserver.utils;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.xiaoazi.futureletterserver.common.AppVariable;
import top.xiaoazi.futureletterserver.module.user.vo.UserRedisVO;

@Component
public class UserUtils {

    @Autowired
    private RedisUtils redisUtils;

    /**
     * 获取当前登录的 userId
     * @param request
     * @return
     */
    public Integer getUserId(HttpServletRequest request) {
        String token = request.getHeader(AppVariable.HEADER_TOKEN);
        return ((UserRedisVO)redisUtils.get(token)).getUserId();
    }

    /**
     * 获取当前登录的 User Redis 的对象
     * @param request
     * @return
     */
    public UserRedisVO getUser(HttpServletRequest request) {
        String token = request.getHeader(AppVariable.HEADER_TOKEN);
        return (UserRedisVO)redisUtils.get(token);
    }
}
